﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Pascdraw
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            text_color = Color.White;
            back_color = Color.Black;
            janela_codigo.Visible = false;

        }
        [DllImport("kernel32.dll")]
        public static extern void AllocConsole();
        codigo janela_codigo = new codigo();
        char[] ascii = {' ', '☺', '☻', '♥', '♦', '♣', '♠', ' ', ' ', '○', ' ', '♂', '♀', ' ', '♫', '☼', '►', '◄', '↕', '‼', '¶', '§', '▬', '↨', '↑', '↓', '→', '←', '∟', '↔', '▲', '▼', ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '⌂', 'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å', 'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', 'ø', '£', 'Ø', '×', 'ƒ', 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '®', '¬', '½', '¼', '¡', '«', '»', '░', '▒', '▓', '│', '┤', 'Á', 'Â', 'À', '©', '╣', '║', '╗', '╝', '¢', '¥', '┐', '└', '┴', '┬', '├', '─', '┼', 'ã', 'Ã', '╚', '╔', '╩', '╦', '╠', '═', '╬', '¤', 'ð', 'Ð', 'Ê', 'Ë', 'È', 'ı', 'Í', 'Î', 'Ï', '┘', '┌', '█', '▄', '¦', 'Ì', '▀', 'Ó', 'ß', 'Ô', 'Ò', 'õ', 'Õ', 'µ', 'þ', 'Þ', 'Ú', 'Û', 'Ù', 'ý', 'Ý', '¯', '´', '­', '±', '‗', '¾', '¶', '§', '÷', '¸', '°', '¨', '·', '¹', '³', '²', '■', ' ' };
        Color back_color = new Color();
        Color text_color = new Color();
        ConsoleColor text_cons_color = ConsoleColor.White;
        ConsoleColor back_cons_color = ConsoleColor.Black;
        private void Form1_Load(object sender, EventArgs e)
        {
            int x = 1;
            int y = 1;
            for (int i = 1; i <= 2000; i++)
            {
                Label x_Label = new Label();
                x_Label.Name = "pixel";
                x_Label.Size = new System.Drawing.Size(8, 12);
                x_Label.BackColor = System.Drawing.Color.Black;
                x_Label.Margin = new Padding(0); ;
                x_Label.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                x_Label.TextAlign = ContentAlignment.MiddleCenter;
                x_Label.BorderStyle = BorderStyle.None;
                x_Label.FlatStyle = FlatStyle.Flat;
                x_Label.Padding = new Padding(0);
               
                if (x < 10) x_Label.Tag = '0' + x.ToString();
                else x_Label.Tag = x.ToString();
                x_Label.Tag = x_Label.Tag.ToString() + ',';
                if (y < 10) x_Label.Tag = x_Label.Tag.ToString() + '0' + y.ToString();
                else x_Label.Tag = x_Label.Tag + y.ToString();

                if (x != 80)
                {
                    x++;
                }
                else
                {
                    x = 1;
                    y++;
                }
                flowLayoutPanel1.Controls.Add(x_Label);
                x_Label.MouseMove += new MouseEventHandler(x_Label_MouseMove);
                x_Label.MouseClick += new MouseEventHandler(x_Label_MouseClick);
            }
            AllocConsole();
            Console.Title = "Test";
        }

        void x_Label_MouseClick(object sender, MouseEventArgs e)
        {
            Label x_label = sender as Label;
            int x = Convert.ToInt16(x_label.Tag.ToString().Remove(2, 3));
            int y = Convert.ToInt16(x_label.Tag.ToString().Remove(0, 3));

            if (e.Button == MouseButtons.Left)
            {
                x_label.ForeColor = text_color;
                x_label.BackColor = back_color;
                x_label.Text = ascii[Convert.ToInt16(toolStripTextBox1.Text)].ToString();
                janela_codigo.set_text(janela_codigo.get_text() + "gotoxy(" + x + "," + y + ");\n write(#" + toolStripTextBox1.Text + "); \n");
                Console.SetCursorPosition(x, y);
                Console.ForegroundColor = text_cons_color;
                Console.BackgroundColor = back_cons_color;
                Console.Write(ascii[Convert.ToInt16(toolStripTextBox1.Text)].ToString());
            }
            if (e.Button == MouseButtons.Right)
            {
               RichTextBox x_rich = janela_codigo.Tag as RichTextBox;
               Boolean startdel = false;
                int i = 0;
                string temp_string = "";
               foreach (string x_string in x_rich.Lines)
               {
                   i++;
                   if ((x_string == "gotoxy(" + x + "," + y + ");") && (startdel == false))
                   {
                       startdel = true;
                   }
                   else if (((x_string.Contains("gotoxy(") == true)|| (x_string.Contains("textcolor")) || (x_string.Contains("textbackground"))) && (startdel == true))
                   {
                       startdel = false;
                       temp_string = temp_string + x_string + "\n";
                   }
                    else if (startdel == false)
                   {
                       temp_string = temp_string + x_string + "\n";
                   }

               }
               int h = 0 ;
               char h_string;
               if (temp_string.Length > 0)
               {
                   do
                   {
                       h++;
                       h_string = temp_string[h];
                   }
                   while ((h_string == '\\') && (h_string == 'n'));
                   x_rich.Text = temp_string.Remove(0, h - 1);
               }
               else
               {
                   x_rich.Text = temp_string;
               }

               Console.SetCursorPosition(x, y);
               Console.BackgroundColor = ConsoleColor.Black;
               x_label.BackColor = Color.Black;
               Console.Write(" ");
               x_label.Text = "";
               
            }
        }

        void x_Label_MouseMove(object sender, MouseEventArgs e)
        {
            Label x_label = sender as Label;
            toolStripStatusLabel1.Text = "gotoxy("+ x_label.Tag.ToString()+")";
        }

        private void cyanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem x_item = sender as ToolStripMenuItem;
            switch (x_item.Text.ToLower())
            {
                case "white":
                    text_color = Color.White;
                    text_cons_color = ConsoleColor.White;
                    break;
                case "blue":
                    text_color = Color.Blue;
                    text_cons_color = ConsoleColor.DarkBlue;
                    break;
                case "green":
                    text_color = Color.Green;
                    text_cons_color = ConsoleColor.DarkGreen;
                    break;
                case "cyan":
                    text_color = Color.Cyan;
                    text_cons_color = ConsoleColor.DarkCyan;
                    break;
                case "red":
                    text_color = Color.Red;
                    text_cons_color = ConsoleColor.DarkRed;
                    break;
                case "magenta":
                    text_color = Color.Magenta;
                    text_cons_color = ConsoleColor.DarkMagenta;
                    break;
                case "brown":
                    text_color = Color.Brown;
                    text_cons_color = ConsoleColor.DarkYellow;
                    break;
                case "lightgray":
                    text_color = Color.Gray;
                    text_cons_color = ConsoleColor.Gray;
                    break;
                case "darkgray":
                    text_color = Color.DarkGray;
                    text_cons_color = ConsoleColor.DarkGray;
                    break;
                case "lightblue":
                    text_color = Color.LightBlue;
                    text_cons_color = ConsoleColor.Blue;
                    break;
                case "lightgreen":
                    text_color = Color.LightGreen;
                    text_cons_color = ConsoleColor.Green;
                    break;
                case "lightcyan":
                    text_color = Color.LightSkyBlue;
                    text_cons_color = ConsoleColor.Cyan;
                    break;
                case "lightred":
                    text_color = Color.IndianRed;
                    text_cons_color = ConsoleColor.Red;
                    break;
                case "lightmagenta":
                    text_color = Color.HotPink;
                    text_cons_color = ConsoleColor.Magenta;
                    break;
                case "yellow":
                    text_color = Color.Yellow;
                    text_cons_color = ConsoleColor.Yellow;
                    break;
                case "black":
                    text_color = Color.Black;
                    text_cons_color = ConsoleColor.Black;
                    break;

            }
            toolStripDropDownButton1.Text = x_item.Text;
            toolStripDropDownButton1.Image = x_item.Image;
            janela_codigo.set_text(janela_codigo.get_text()+"textcolor("+x_item.Text+");\n");
        }

        private void brownToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem x_item = sender as ToolStripMenuItem;
            switch (x_item.Text.ToLower())
            {
                case "white":
                    back_color = Color.White;
                    back_cons_color = ConsoleColor.White;
                    break;
                case "blue":
                    back_color = Color.Blue;
                    back_cons_color = ConsoleColor.DarkBlue;
                    break;
                case "green":
                    back_color = Color.Green;
                    back_cons_color = ConsoleColor.DarkGreen;
                    break;
                case "cyan":
                    back_color = Color.Cyan;
                    back_cons_color = ConsoleColor.DarkCyan;
                    break;
                case "red":
                    back_color = Color.Red;
                    back_cons_color = ConsoleColor.DarkRed;
                    break;
                case "magenta":
                    back_color = Color.Magenta;
                    back_cons_color = ConsoleColor.DarkMagenta;
                    break;
                case "brown":
                    back_color = Color.Brown;
                    back_cons_color = ConsoleColor.DarkYellow;
                    break;
                case "black":
                    back_color = Color.Black;
                    back_cons_color = ConsoleColor.Black;
                    break;

            }
            toolStripDropDownButton2.Text = x_item.Text;
            toolStripDropDownButton2.Image = x_item.Image;
            janela_codigo.set_text(janela_codigo.get_text()+"textbackground(" + x_item.Text + ");\n");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        private void cODIGOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            janela_codigo.Visible = true;
        }

    }
}
